package com.lucky.ebidding.service;

import com.lucky.ebidding.dao.NotificationDao;
import com.lucky.ebidding.dao.TbInfoDao;
import com.lucky.ebidding.dao.ZbInfoDao;
import com.lucky.ebidding.model.*;
import com.lucky.ebidding.service.interfaces.TbServicel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * ClassName:TbService
 * Package:com.lucky.ebidding.service
 * Description:
 *
 * @Date:2020/4/21 10:32
 * @Author:857251389@qq.com
 */
@Service
public class TbService implements TbServicel {

    @Autowired
    private TbInfoDao tbInfoDao;

    @Autowired
    private ZbInfoDao zbInfoDao;

    @Autowired
    private NotificationDao notificationDao;

    @Autowired
    private NotificationService notificationService;

    @Override
    public void insert(TbInfo tbInfo) {
        tbInfo.setProjectId(UUID.randomUUID().toString().substring(0, 8)+ LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        int i = tbInfoDao.insertSelective(tbInfo);
    }

    @Override
    public List<TbInfo> selectTbInfosById(Integer id) {

        ZbInfoExample zbInfoExample = new ZbInfoExample();
        zbInfoExample.createCriteria().andUserIdEqualTo(id);

        List<ZbInfo> zbInfos = zbInfoDao.selectByExample(zbInfoExample);

        List<Integer> zbIds = zbInfos.stream().map(zbInfo -> zbInfo.getId()).collect(Collectors.toList());
        TbInfoExample tbInfoExample = new TbInfoExample();
        tbInfoExample.createCriteria().andZbIdIn(zbIds);
        List<TbInfo> tbInfos = tbInfoDao.selectByExample(tbInfoExample);

        return tbInfos;
    }

    @Override
    public List<TbInfo> selectTbInfoById(Integer id) {
//        ZbInfo zbInfo = zbInfoDao.selectByPrimaryKey(id);
        TbInfoExample tbInfoExample = new TbInfoExample();
        tbInfoExample.createCriteria().andZbIdEqualTo(id);
        List<TbInfo> tbInfos = tbInfoDao.selectByExample(tbInfoExample);

        return tbInfos;
    }

    @Override
    public int setStatus(Integer tbId, Integer status,Integer zbId,User user) {
        TbInfo tbInfo = new TbInfo();
        tbInfo.setId(tbId);
        tbInfo.setQualFlag(status);
        int i = tbInfoDao.updateByPrimaryKeySelective(tbInfo);
        //审核之后发送通知
        TbInfo tbInfo1 = tbInfoDao.selectByPrimaryKey(tbId);
        String content = "";
        if(status == 1){
            content = "通过";
        }else{
            content = "不通过";
        }
        notificationService.sendNotification(tbInfo1.getUserId(),user.getId(),0,zbId,content);
        return i;
    }

    @Override
    public TbInfo selectTbInfoByIdAndZbId(Integer userId, Integer zbId) {
        TbInfoExample tbInfoExample = new TbInfoExample();
        tbInfoExample.createCriteria().andUserIdEqualTo(userId).andZbIdEqualTo(zbId);
        List<TbInfo> tbInfos = tbInfoDao.selectByExample(tbInfoExample);
        if(tbInfos.size()>0){
                return tbInfos.get(0);
        }else {
//            TbInfo tbInfo = new TbInfo();
            return null;
        }
    }

    @Override
    public void update(TbInfo tbInfo) {
        tbInfoDao.updateByPrimaryKeySelective(tbInfo);
    }

    @Override
    public TbInfo selectById(Integer tbId) {
        TbInfo tbInfo = tbInfoDao.selectByPrimaryKey(tbId);
        return tbInfo;
    }

    @Override
    public List<TbInfo> selectByBedFlag(int i) {

        TbInfoExample tbInfoExample = new TbInfoExample();
        tbInfoExample.createCriteria().andBeddingFlagEqualTo(0);
        List<TbInfo> tbInfos = tbInfoDao.selectByExample(tbInfoExample);
        return tbInfos;
    }


}
